import requests
from bs4 import BeautifulSoup
import csv
from time import sleep
from time import time

list_url = "https://movie.douban.com/top250?start={0}&filter="
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}

time_start = time()
csv_file = open(f"movie-{time_start}.csv", "w", newline="", encoding="utf-8")
csv_writer = csv.writer(csv_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(["name", "link", "genre", "director", "actors", "releaseTime", "runTime", "star"])
for i in range(0, 250, 25):
    print(list_url.format(i))
    response = requests.get(list_url.format(i), headers=headers)
    # print(response.text)
    soup = BeautifulSoup(response.text, 'html.parser')
    # print(soup)
    # print(soup.find_all('div', attrs={'class': 'hd'}))
    for tag in soup.find_all('div', attrs={'class': 'hd'}):
        # print(tag)
        # print(tag.a)
        # print(tag.a.span)
        # print(tag.a.span.text)
        try:
            movie_url = tag.a['href']
            movie_name = tag.a.span.text
            print(movie_name, movie_url)
            movie_resp = requests.get(movie_url, headers=headers)
            movie_soup = BeautifulSoup(movie_resp.text, 'html.parser')

            movie_genre = []
            for p in movie_soup.find_all('span', attrs={'property': 'v:genre'}):
                movie_genre.append(p.text)

            movie_actors = []
            for p in movie_soup.find_all('a', attrs={'rel': 'v:starring'}):
                movie_actors.append(p.text)

            movie_release = movie_soup.find('span', attrs={'property': 'v:initialReleaseDate'}).text
            movie_avg = movie_soup.find('strong', attrs={'property': 'v:average'}).text
            movie_director = movie_soup.find('a', attrs={'rel': 'v:directedBy'}).text
            movie_time = movie_soup.find('span', attrs={'property': 'v:runtime'}).text
            print(movie_genre, movie_actors, movie_release, movie_avg, movie_director, movie_time)
            csv_writer.writerow(
                [movie_name, movie_url, movie_genre, movie_director, movie_actors, movie_release, movie_time,
                 movie_avg])
            sleep(1)
        except:
            print("Error")
            pass
